/*
	File:				Entity.cpp
	Course:		SGP 1208
	Author:		Kori Po
	Purpose:	Base Class for all in-game objects
*/

#pragma once

#include "Entity.h"
#include "Game.h"
#include "EventSystem.h"

// SGD Wrappers
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/SGD_Math.h"
#include <cassert>

CEntity::CEntity(void)
{
	// Assign data members to 'clean' values
	m_nType			= OBJ_BASE;

	m_nImageID	= -1;
	m_nWidth			= 1;
	m_nHeight		= 1;
	m_fX					= 0;
	m_fY					= 0;
	m_fVelX				= 0;
	m_fVelY				= 0;
	m_bFacing		= true;

	this->m_fRotation = 0.0f;

	m_nRefCount	= 1;
}


CEntity::~CEntity(void)
{
}

/*virtual*/ void CEntity::Update( float fElapsedTime )
{
	// Move the token by its velocity each second
	m_fX += m_fVelX * fElapsedTime;
	m_fY += m_fVelY * fElapsedTime;
}

/*virtual*/ void CEntity::Render( void )
{
	assert( m_nImageID != -1 && "CEntity does not have an image!" );
	
	int ToDrawX = int(GetX()) - int(GetWidth()/2);
	int ToDrawY = int(GetY()) - int(GetHeight()/2);

		CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pTM->Draw( GetImageID(), ToDrawX, ToDrawY, 1.0f, 1.0f, &m_SourceRect,
			   float(GetWidth()/2), float(GetHeight()/2), GetRotation() );
	/*
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
<<<<<<< HEAD

	pTM->Draw( GetImageID(), int(GetX()), int(GetY()), 1.0f, 1.0f, nullptr,
=======
<<<<<<< HEAD
	pTM->Draw( GetImageID(), int(GetX()), int(GetY()), 1.0f, 1.0f, &m_SourceRect,
>>>>>>> ba596b7ea133646374f489caa24ee0458d0c0cf0
			   float(GetWidth()/2), float(GetHeight()/2), GetRotation() );
			   */

	pTM->Draw( GetImageID(), int(GetX())-this->GetWidth()/2, int(GetY())-this->GetHeight()/2, 1.0f, 1.0f, &m_SourceRect,
			this->GetWidth(), this->GetHeight(), GetRotation() );

}

/*virtual*/ RECT CEntity::GetRect( void ) const
{
	// Create a rect using the object's position as the center
	RECT rSelf = 
	{
		(LONG)(this->GetX() - this->GetWidth()/2), 
		(LONG)(this->GetY() - this->GetHeight()/2),
		(LONG)(this->GetX() + this->GetWidth()/2), 
		(LONG)(this->GetY() + this->GetHeight()/2)
	};

	return rSelf;
}

/*virtual*/ bool CEntity::CheckCollision( IEntity* pOther )
{
	// Us
	RECT rSelf = GetRect(); 
	// Them
	RECT rOther = pOther->GetRect();
	// Check
	RECT rOverlap;
	
	CEntity* pOtherGuy = dynamic_cast< CEntity* >( pOther );

	RECT TestingSelf = {rSelf.left, rSelf.top + this->GetHeight()/2, rSelf.right, rSelf.bottom};
	RECT TestingOther = {rOther.left, rOther.top  + pOtherGuy->GetHeight()/2 , rOther.right, rOther.bottom};

	BOOL bColliding = IntersectRect( &rOverlap, &TestingSelf, &TestingOther );
	
	if( bColliding == TRUE )
	{
		if(GetType() == OBJ_PLAYER && pOther->GetType() == OBJ_ENEMY)
		{
			CEventSystem::GetInstance()->SendEvent( "Go Red", this );
		}
		return true;
	}
	else
		return false;
}

// Reference counting
// - ONLY done inside CEntity!
/*virtual*/ void CEntity::AddRef( void )
{
	++m_nRefCount;
}

/*virtual*/ void CEntity::Release( void )
{
	--m_nRefCount;


	// Self-destruct?
	if( m_nRefCount == 0 )
		delete this;
}